package com.sann.cms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sann.cms.entity.Banner;
import com.sann.cms.service.BannerService;
import com.sann.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author sann
 * @since 2021-05-04
 */
@Api(tags = "网站后台banner管理接口")
@RestController
@RequestMapping(path = "/cmsservice/banner/admin")
@CrossOrigin
public class BannerAdminController {
    @Autowired
    private BannerService bannerService;

    @ApiOperation(value = "获取Banner分页列表")
    @GetMapping("/pageBanner/{pageNo}/{pageSize}")
    public R pageBanner(@ApiParam(name = "pageNo",value = "当前页码", required = true)
            @PathVariable Integer pageNo,
            @ApiParam(name = "pageSize", value = "分页大小", required = true)
            @PathVariable Integer pageSize){
        Page<Banner> pageBanner = null;
        try {
            pageBanner = new Page<>(pageNo, pageSize);
            pageBanner = this.bannerService.page(pageBanner, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().data("total", pageBanner.getTotal()).data("rows",pageBanner.getRecords());
    }

    @ApiOperation(value = "Banner的Id")
    @GetMapping(path = "/get/{id}")
    public R get(@PathVariable String id){
        Banner banner = null;
        try {
            banner = this.bannerService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().data("item", banner);
    }

    @ApiOperation(value = "新增Banner")
    @PostMapping(path = "/save")
    @CacheEvict(value = "banner", allEntries = true)
    public R save(@RequestBody Banner banner){
        try {
            boolean bol = this.bannerService.save(banner);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    @ApiOperation(value = "修改Banner")
    @PostMapping(path = "/updateById")
    @CacheEvict(value = "banner", allEntries = true)
    public R updateById(@RequestBody Banner banner){
        try {
            boolean bol = this.bannerService.updateById(banner);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping(path = "/deleteById/{id}")
    @CacheEvict(value = "banner", allEntries = true)
    public R deleteById(@PathVariable String id){
        try {
            boolean bol = this.bannerService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

}

